﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Planisoft.BL;
using Planisoft.Entities;
using Microsoft.Practices.Unity;
using PagedList.Mvc;
using PagedList;

namespace Planisoft.ClienteMvcWeb.Controllers.Dependiente
{
    public class DependienteController : Controller
    {
        //
        // GET: /Dependiente/
        [Dependency]
        public ItrabajadorBL TrabajadorBL { get; set; }

        [Dependency]
        public IDependientesBL DependientesBL { get; set; }
        
        [Dependency]
        public ITipoDocumentoIdBL TipoDocumentoIdBL { get; set; }

        [Dependency]
        public IUbigeoDepartamentoBL UbigeoDepartamentoBL { get; set; }
        [Dependency]
        public IUbigeoProvinciaBL UbigeoProvinciaBL { get; set; }
        [Dependency]
        public IUbigeoDistritoBL UbigeoDistritoBL { get; set; }
        [Dependency]
        public IUbigeoViaBL UbigeoViaBL { get; set; }
        [Dependency]
        public IUbigeoZonaBL UbigeoZonaBL { get; set; }

        [Dependency]
        public IVinculoFamiliarBL VinculoFamiliarBL { get; set; }
        
        public ActionResult Index()
        {
            return View();
        }

        //pagina principal de dependientes, para ser trabaja con PartialView
        //[Authorize(Roles="Contador")]
        public ActionResult IdexDependiente(string criterio,Int32?page)
        {
            var pageNumero = page ?? 1;
            var Listatrabajador = TrabajadorBL.GetAllFromTrabajadorByCriterio(criterio, pageNumero, 3);
            return Request.IsAjaxRequest() ? PartialView("_ListaTrabajador", Listatrabajador) as ActionResult : View(Listatrabajador);

        }

        
        //PartialView
        public ActionResult ListaDependientesTrabajador(Int32 idTrabajador,Int32? pagi)
        {
            var pageNumero = pagi ?? 1;
            var listaDependientes = DependientesBL.GetAllFromDependienteByIdTrabajador(idTrabajador, pageNumero, 2);

            ViewBag.IdTrabajador = idTrabajador;
            return PartialView("_ListaDependientesTrabajador",listaDependientes);
        }

      
        
        //PartialView
        [HttpGet]
        public ActionResult AgregarDependiente(int id)
        {
            var listTipoDocumentoId = TipoDocumentoIdBL.GetALLFromTipoDocumento();
            ViewData["IdTipoDocumento"] = new SelectList(listTipoDocumentoId, "IdTipoDocumento", "DescTipDoc");
            
            var listVinculoFamiliar = VinculoFamiliarBL.GetAllFromVinculoFamiliar();
            ViewData["IdVinculoFam"] = new SelectList(listVinculoFamiliar, "IdVinculoFam", "DescVincFam");

            var listUbigeoDepartamento = UbigeoDepartamentoBL.GetALLFromUbigeoDepartamento();
            ViewData["idUbiDpto"] = new SelectList(listUbigeoDepartamento, "idUbiDpto", "DescUbiDpto");

            var listUbigeoProvincia = UbigeoProvinciaBL.GetAllFromUbigeoProvincia();
            ViewData["IdUbiProv"] = new SelectList(listUbigeoProvincia, "IdUbiProv", "DescUbiProv");

            var listUbigeoDistrito = UbigeoDistritoBL.GetAllFromUbigeoDistrito();
            ViewData["IdUbiDtto"] = new SelectList(listUbigeoDistrito, "IdUbiDtto", "DescUbiDtto");

            var listUbigeoZona = UbigeoZonaBL.GetAllFromUbigeoZona();
            ViewData["IdZona"] = new SelectList(listUbigeoZona, "IdZona", "DescUbiZona");

            var listUbigeoVia = UbigeoViaBL.GetAllFromUbigeoVia();
            ViewData["IdVia"] = new SelectList(listUbigeoVia, "IdVia", "DescUbiVia");
            
            return PartialView("_AgregarDependiente");
        }

        [HttpPost]
        public ActionResult AgregarDependiente(int id,Planisoft.Entities.Dependiente dependiente)
        {
            
            try
            {
                var listTipoDocumentoId = TipoDocumentoIdBL.GetALLFromTipoDocumento();
                ViewData["IdTipoDocumento"] = new SelectList(listTipoDocumentoId, "IdTipoDocumento", "DescTipDoc");

                var listVinculoFamiliar = VinculoFamiliarBL.GetAllFromVinculoFamiliar();
                ViewData["IdVinculoFam"] = new SelectList(listVinculoFamiliar, "IdVinculoFam", "DescVincFam");

                var listUbigeoDepartamento = UbigeoDepartamentoBL.GetALLFromUbigeoDepartamento();
                ViewData["idUbiDpto"] = new SelectList(listUbigeoDepartamento, "idUbiDpto", "DescUbiDpto");

                var listUbigeoProvincia = UbigeoProvinciaBL.GetAllFromUbigeoProvincia();
                ViewData["IdUbiProv"] = new SelectList(listUbigeoProvincia, "IdUbiProv", "DescUbiProv");

                var listUbigeoDistrito = UbigeoDistritoBL.GetAllFromUbigeoDistrito();
                ViewData["IdUbiDtto"] = new SelectList(listUbigeoDistrito, "IdUbiDtto", "DescUbiDtto");

                var listUbigeoZona = UbigeoZonaBL.GetAllFromUbigeoZona();
                ViewData["IdZona"] = new SelectList(listUbigeoZona, "IdZona", "DescUbiZona");

                var listUbigeoVia = UbigeoViaBL.GetAllFromUbigeoVia();
                ViewData["IdVia"] = new SelectList(listUbigeoVia, "IdVia", "DescUbiVia");
                dependiente.IdTrabajador = id;
                
                DependientesBL.RegistrarDependiente(dependiente);

                return RedirectToAction("IdexDependiente");
            }
            catch (Exception)
            {
                
                throw;
            }
            
        }

        public ActionResult EliminarDependienteTrabajador(int id, Int32 idTrabajador)
        {
            
            DependientesBL.EliminarDependiente(id);

            //return ListaDependientesTrabajador(id,null);

            return RedirectToAction("IdexDependiente");
        }

        [HttpGet]
        public ActionResult Actualizar(Int32 id)
        {
            var dependiente = DependientesBL.GetFromDependienteById(id);
            return PartialView("_Actualizar",dependiente);
        }
        [HttpPost]
        public ActionResult Actualizar(int id, Planisoft.Entities.Dependiente dependiente)
        {
            try
            {
                DependientesBL.actualizar(dependiente);
                //return PartialView("_AgregarDependiente", dependiente);
                return RedirectToAction("IdexDependiente");
            }
            catch (Exception)
            {

                throw;
            }

        }

        public ActionResult AgregarDependienteTest(Int32 id)
        {
            return View();
        }

        
    }
}
